<?php

class Con_users extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library('session');

        $this->load->database();
        $this->load->model('model_users');
    }

    function index() {
        $data['mytitle'] = "Mainpage";
        $this->load->view('view_mainpage', $data);
    }

    function login() {
        $data['mytitle'] = "Mainpage";
        if ((isset($_POST['username']) || isset($_POST['password'])) == "") {
            $this->load->view('view_mainpage', $data);
        } else {
            $data['sql_name'] = $this->model_users->select_name();
            $data['data'] = $this->model_users->select_login();
            $this->load->view('view_mainpage_menu', $data);
        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect('con_users');
    }

    function registers() {
        $data['mytitle'] = "Registers";

        if (count($_POST) > 0) { // input type post > true
            $myname = $_POST['myname'];
            $mysurname = $_POST['mysurname'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];

            // check data not null
            if ($myname != "" && $mysurname != "" && $username != "" && $password != "" && $email != "") {
                $this->model_users->insert_registers();
                $this->load->view('view_registers_success', $data);
            } else { // data = null
                $this->load->view('view_registers_error', $data);
            }
        } else { // input type post > false
            $this->load->view('view_registers', $data);
        }
    }

    //------------------------------------------------------------------------------
// start personmanager
    function active_person() {
        $data['mytitle'] = "Active Person";

        if (isset($_POST['select_type']) != null) {
            $data['data'] = $this->model_users->select_person_type();
            $this->load->view('view_active_person', $data);
        } else {
            if (isset($_POST['update_status_person_id']) != null) {
                $this->model_users->update_active_users();
            }

            if (isset($_POST['delete_person']) != null) {
                $this->model_users->delete_active_users();
            }

            $data['data'] = $this->model_users->select_person();
            $this->load->view('view_active_person', $data);
        }
    }

   /* function edit_person() {
        if (isset($_POST['edit_person']) != null) {
            $data['mytitle'] = "Admin Edit Person";
            $this->load->view('view_admin_edit_person', $data);
        }
    }*/

    
    function Newperson() {
        $data['qry_group']= $this->model_users->select_group();
        $data['qry_personid'] = $this->model_users->select_personid();
        $this->load->view('View_newperson',$data);                   
    }
    
    function Addperson() {
        $this->model_users->insert();
        
        $data['data']= $this->model_users->select_person();
        $this->load->view('view_active_person',$data);                    
    }
    
    function Editpersonpage() {
        if (isset($_POST['editperson']) != null) { 
                $data['data'] = $this->model_users->select_edit();
                $data['qry_group']= $this->model_users->select_group();
                $data['qry_chkgroup']= $this->model_users->select_chkgroup();
                $this->load->view('view_editperson', $data);
        }
    }
    
    function Editperson() {
        $data['show'] = $this->model_users->edit_users();
        
        $data['data'] = $this->model_users->select_person();
        $this->load->view('view_active_person', $data);
    }
//------------------------------------------------------------------------------
// start groupmanager
    
    function Group_person() {
        if (isset($_POST['select_type']) != null) {
            $data['data'] = $this->model_users->select_gperson_type();
            $this->load->view('view_groupmanager', $data);
        } else {
            if (isset($_POST['activegperson']) != null) { 
                $data['show'] = $this->model_users->update_active_groupperson();
            }
            if (isset($_POST['deletegperson']) != null) { 
                $data['show'] = $this->model_users->delete_groupperson();
            }
        }
        
        $data['data'] = $this->model_users->select_group();
        $this->load->view('view_groupmanager', $data);  
    }
    
    function Newgperson() {
        $this->load->view('View_newgroup');                   
    }
    
    function Addgperson() {
        $this->model_users->insertgroup();
        
        $data['data']= $this->model_users->select_group();
        $this->load->view('view_groupmanager',$data);                    
    }
    
    function Editgpersonpage() {
        if (isset($_POST['editgperson']) != null) { 
                $data['data'] = $this->model_users->select_editgroup();
                $this->load->view('view_editgroup', $data);
        }
    }
    
    function Editgperson() {
        $data['show'] = $this->model_users->edit_users();
        
        $data['data'] = $this->model_users->select_group();
        $this->load->view('view_groupmanager', $data);
    }
    
//------------------------------------------------------------------------------
// start menumanager

    function Menu() {
        $data['mytitle'] = "MENU";
        if (isset($_POST['select_menu_all_id']) != null) {
            $data['menumanag'] = $this->model_users->select_menu_all_type();
            $this->load->view('view_menumanager', $data);
        } else {
            if (isset($_POST['activemenu']) != null) {
                $data['testing'] = $this->model_users->active_menu();
            }
            if (isset($_POST['deletemenu']) != null) {
                $data['testing'] = $this->model_users->delete_menu();
            }
            $data['menumanag'] = $this->model_users->select_menu();
            // get groupmenu in listbox
            $data['gmenu']= $this->model_users->select_gmenu();
            $this->load->view('view_menumanager', $data);
        }
         
    } 

   //select menu form gropmenu  
   function show_menu() {
       $data['mytitle'] = "Menu";
        $data['menumanag']= $this->model_users->select_menu_of_group();
        $data['gmenu']= $this->model_users->select_gmenu();
        $this->load->view('view_menumanager',$data);
   }
    function new_menu() {
       $data['mytitle'] = "New Menu";
       $data['group']= $this->model_users->select_gmenu();
       $data['menuid']= $this->model_users->select_menuid_max();
        $this->load->view('view_newmenu',$data);
        
    } 
    
    function new_menu2() {
         $data['mytitle'] = "New Menu";
        $this->model_users->new_menu();  
        $this->menu(); 
    }
    
    function edit_menu() { 
         $data['mytitle'] = "Edit Menu";         
        $data['edit']= $this->model_users->edit_menu();  
        $data['group']= $this->model_users->select_gmenu();
        $data['chkgroup']= $this->model_users->chk_group_menu();
        $this->load->view('view_editmenu',$data);
    } 
    
    function edit_menu2(){
        $data['mytitle'] = "Edit Menu";
        $this->model_users->edit_menu2();  
        $this->menu();   
    }
//------------------------------------------------------------------------------
// start GROUPmenumanager    
    function group_menu() {
        $data['mytitle'] = "Group Menu";
        if (isset($_POST['select_type']) != null) {
            $data['gmenu'] = $this->model_users->select_gperson_type();
            $this->load->view('view_groupmenu', $data);
        } else {
            if (isset($_POST['activegmenu']) != null) { 
                $data['testing'] = $this->model_users->active_gmenu();
            }
            if (isset($_POST['deletegmenu']) != null) { 
                $data['testing'] = $this->model_users->delete_gmenu();
            }
        }
        
        $data['gmenu'] = $this->model_users->select_gmenu();
        $this->load->view('view_groupmenu', $data);
    } 
    
    function new_gmenu(){
        $data['mytitle'] = "New Group Menu";
        $this->load->view('view_newgroupmenu');
    } 
    
    function new_gmenu2() {
        $data['mytitle'] = "New Group Menu";
        $this->model_users->new_gmenu();  
        $this->group_menu(); 
    }
    
    function edit_gmenu() {
        $data['mytitle'] = "Edit Group Menu";
        $data['edit']= $this->model_users->edit_gmenu(); 
        $this->load->view('view_editgroupmenu', $data);    
    } 
    
    function edit_gmenu2(){
        $data['mytitle'] = "Edit Group Menu";
        $this->model_users->edit_gmenu2();  
        $this->group_menu();   
    }

}

?>
